home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / sim.lha / sim / init.c < prev    next >
C/C++ Source or Header  |  1990-04-12  |  8KB  |  263 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* init.c */
  25.  
  26. #include "sim.h"
  27. #include "inst.h"
  28. #include "aux.h"
  29. #include <strings.h>
  30.  
  31. /* #define DEBUG */
  32.  
  33. LONG  maxmem    = 500000;  
  34. LONG  maxpspace = 300000;
  35. LONG  maxtrail;
  36.  
  37. WORD d_trace, d_hitrace;
  38.  
  39. extern char *malloc(), *getenv();
  40. extern LONG_PTR insert();
  41.  
  42. /****************************************************************************/
  43.  
  44. static BYTE perm = PERM;
  45.  
  46. init_simpath(path)
  47. LONG *path;
  48. {
  49.     CHAR *ch;
  50.     LONG_PTR temp;
  51.  
  52.    if ((ch = getenv("SIMPATH")) == NULL)
  53.     quit("SB-Prolog: SIMPATH not defined: cannot proceed\n");
  54.    temp = insert(ch, strlen(ch), 0, &perm);
  55.    *path = ((LONG)temp) | CS_TAG;
  56. }
  57.  
  58. init_sim(argc, argv)
  59. int  argc;
  60. char *argv[];
  61. {
  62.    WORD     i, itmp; 
  63.    LONG     tempint;
  64.    LONG_PTR temp, insert();
  65.    CHAR     c;
  66.    CHAR_PTR charp;
  67.  
  68.    /* for debugging malloc on sun (include /usr/lib/debug/malloc.o in link)
  69.     * malloc_debug(2);
  70.    */
  71.  
  72.    for (i = 0; i < BUCKET_CHAIN; i++) {
  73.       MAKE_FREE(LONG_PTR, hash_table[i][TEMP]);
  74.       MAKE_FREE(LONG_PTR, hash_table[i][PERM]);
  75.    }
  76.  
  77.    trace = hitrace = disassem = num_line = 0;
  78.    trace_sta = d_trace = d_hitrace = call_intercept = 0;
  79.    interrupt_code = 0;
  80.  
  81.    maxtrail = maxmem / 5;
  82.  
  83.    if (argc == 1)
  84.       quit("Usage: sim [-Ttdns] [-m s_size] [-p p_size] [-b tr_size] [-ui num] bc_file ...\n");
  85.  
  86.    for (i = 1; i < argc; i++) {
  87.       if (*argv[i] == '-') {
  88.          charp = argv[i];
  89.          while (c = *++charp) {
  90.             switch (c) {
  91.                case 't': trace = d_trace = 1;
  92.                          break;
  93.                case 'T': hitrace = d_hitrace = 1;
  94.                case 's': trace_sta = 1;
  95.                          call_intercept = 1;
  96.                          break;
  97.                case 'n': num_line = 1;
  98.                          break;
  99.                case 'd': disassem = 1;
  100.                          break;
  101.                case 'm': i++;
  102.                          sscanf(argv[i], "%d", &maxmem);
  103.                          maxtrail = maxmem / 5;
  104.                          break;
  105.                case 'p': i++;
  106.                          sscanf(argv[i], "%d", &maxpspace);
  107.                          break;
  108.                case 'b': i++;
  109.                          sscanf(argv[i], "%d", &maxtrail);
  110.                          break;
  111.                case 'u': i++;    /* skip user field on this pass */
  112.                          charp++;
  113.                          break;
  114.                default : printf("Unknown option %c\n", c);
  115.             }  /* switch */
  116.          }  /* while */
  117.       }  /* if */
  118.    }  /* for */
  119.  
  120.    pspace = (LONG_PTR)(malloc(maxpspace*sizeof(LONG)));
  121.    if (!pspace)
  122.       quit("Not enough core!\n");
  123.    max_fence = (CHAR_PTR)(pspace + maxpspace);
  124.  
  125.    memory = (LONG_PTR)(malloc(maxmem*sizeof(LONG)));
  126.    if (!memory)
  127.       quit("Not enough core!\n");
  128.  
  129.    tstack = (LONG_PTR)(malloc(maxtrail*sizeof(LONG)));
  130.    if (!tstack)
  131.       quit("Not enough core!\n");
  132.  
  133.    heap_bottom  = memory;
  134.    local_bottom = memory + maxmem;
  135.    trail_bottom = tstack + maxtrail;
  136.  
  137.    breg  = ereg      = mlocaltop = local_bottom - 2;
  138.    hreg  = mheaptop  = heap_bottom;
  139.    trreg = mtrailtop = trail_bottom - 1;
  140.    hbreg = heap_bottom - 1;
  141.    trap_vector[0] = pspace;
  142.    curr_fence     = (CHAR_PTR)pspace;
  143.  
  144.    /* install fail and halt instructions     */
  145.    /* format is  opcode + pad  (word + word) */
  146.  
  147.    *((WORD_PTR)curr_fence) = fail;   curr_fence += sizeof(WORD);
  148.    *((WORD_PTR)curr_fence) = 0;      curr_fence += sizeof(WORD);
  149.  
  150.    cpreg = (LONG_PTR)curr_fence;                /* halt on final success */
  151.    *(local_bottom - 1) = (LONG)curr_fence;      /* halt on final failure */
  152.  
  153.    *((WORD_PTR)curr_fence) = halt;   curr_fence += sizeof(WORD);
  154.    *((WORD_PTR)curr_fence) =    0;   curr_fence += sizeof(WORD);
  155.  
  156.    temp = insert("[]", 2, 0, &perm);
  157.    nil_sym = (LONG)temp | CS_TAG;
  158.  
  159.    temp = insert(".", 1, 2, &perm);
  160.    list_str = (LONG)temp | CS_TAG;
  161.    list_psc = (PSC_REC_PTR)FOLLOW(temp);
  162.  
  163.    temp = insert(",", 1, 2, &perm);
  164.    comma_psc = (PSC_REC_PTR)FOLLOW(temp);
  165.  
  166.    temp = insert("_$interrupt", 11, 2, &perm);
  167.    interrupt_psc = (PSC_REC_PTR)FOLLOW(temp);
  168.    trap_vector[1] = 0;
  169.    inst_begin = 0;
  170.  
  171.    /* now strip off user parameters */
  172.    for (i = 0; i < 10;  i++) 
  173.       flags[i] = nil_sym;
  174.    for (i = 1; i < argc; i++) {
  175.       if (*argv[i] == '-') {
  176.          charp = argv[i];
  177.          while (c = *++charp) {
  178.             switch (c) {
  179.                case 'p':
  180.                case 'b':
  181.                case 'm': i++;
  182.                case 't':
  183.                case 'T':
  184.                case 's':
  185.                case 'n':
  186.                case 'd': break;
  187.                case 'u': itmp = *++charp - '0';
  188.                          i++;  /* value; retrieved by flags(index+10,V) */
  189.                          if (*argv[i] >= 48 && *argv[i] <= 57) {  /*num*/
  190.                             sscanf(argv[i], "%d", &tempint);
  191.                             flags[itmp] = MAKEINT(tempint);
  192.                          } else {   /* make it a constant */
  193.                             temp = insert(argv[i], strlen(argv[i]), 0, &perm);
  194.                             flags[itmp] = (LONG)temp | CS_TAG;
  195.                          }
  196.                          break;
  197.             }  /* switch */
  198.          }  /* while */
  199.       }  /* if */
  200.    }  /* for */
  201.  
  202. #ifdef DEBUG
  203.    printf("init_sys\n");
  204.    printf("pspace       = %08x\n", pspace);
  205.    printf("max_fence    = %08x\n", max_fence);
  206.    printf("memory       = %08x\n", memory);
  207.    printf("tstack       = %08x\n", tstack);
  208.    printf("heap_bottom  = %08x\n", heap_bottom);
  209.    printf("local_bottom = %08x\n", local_bottom);
  210.    printf("trail_bottom = %08x\n", trail_bottom);
  211.    printf("breg         = %08x\n", breg);
  212.    printf("ereg         = %08x\n", ereg);
  213.    printf("mlocaltop    = %08x\n", mlocaltop);
  214.    printf("hreg         = %08x\n", hreg);
  215.    printf("mheaptop     = %08x\n", mheaptop);
  216.    printf("trreg        = %08x\n", trreg);
  217.    printf("mtrailtop    = %08x\n", mtrailtop);
  218.    printf("hbreg        = %08x\n", hbreg);
  219.    printf("curr_fence   = %08x\n", curr_fence);
  220.    printf("cpreg        = %08x\n", cpreg);
  221.    printf("list_str     = %08x\n", list_str);
  222.    printf("list_psc     = %08x\n", list_psc);
  223.    printf("comma_psc    = %08x\n", comma_psc);
  224. #endif
  225.  
  226. }  /* end of init_sym */
  227.  
  228. /******************************************************************************/
  229.  
  230. init_loading(argc, argv)
  231. int  argc;
  232. char *argv[];
  233. {
  234.    CHAR c;
  235.    WORD i;
  236.    WORD n = 0;  /* number of the initial loaded files */
  237.  
  238.    for (i = 1; i < argc; i++) {
  239.       if (*argv[i] == '-') {
  240.          c = *(argv[i] + 1);
  241.          switch (c) {
  242.             case 'u':
  243.             case 'm':
  244.             case 'p':
  245.             case 'b': i++;
  246.             default : break;
  247.          }
  248.       } else {
  249.          int loaded_ok = loader(argv[i]);
  250.          if (loaded_ok==10) {
  251.             char errormsg[128];
  252.             sprintf(errormsg, "File '%s' cannot be opened\n",argv[i]);
  253.             quit(errormsg);
  254.          } else if (loaded_ok)
  255.                    quit("Error in loading initial files\n");
  256.          n++;
  257.       }
  258.    }
  259.    if (n == 0) 
  260.       quit("No input file!\n");
  261.  
  262. }  /* end of init_loading */
  263.